const app = getApp();
const constants = require('../../../common/Constants.js');
const network_utils = require('../../../http/NetworkUtils.js');
import * as umfToast from '../../../utils/UmfToast';

var pageNo = "1";

Page({
    data: {
        myClerksArr: [],
        hasMore: false
    },

    /**
     * 返回该页面时要刷新页面
     */
    onShow: function () {
        pageNo = "1";
        this._requestMyClerks();
    },

    tapInvite: function (ress) {
        let userId = ress.currentTarget.id;
        this._requestInviteClerk(userId);
    },

    /**
     * 请求我的业务员列表
     * [request]：agentManagement/salesmanList
     * @private
     */
    _requestMyClerks: function () {
        let params = {
            pageNo: pageNo,
            pageSize: app.globalData.pageSize
        };
        network_utils.buildRequest(constants.Interfaces.FUNC_SALESMAN_LIST, params, {
            doHttpSucess: (response) => {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    let clerksData = response["salesmanList"];
                    let hasMore = clerksData.length === parseInt(app.globalData.pageSize);
                    this.setData({
                        myClerksArr: pageNo === "1" ? this._processData(clerksData) : this._processData(this.data.myClerksArr.concat(clerksData)),
                        hasMore: hasMore
                    });

                    if (hasMore) {
                        pageNo = (parseInt(pageNo) + 1).toString();
                    } else {
                        umfToast.showToast("已经加载完所有数据");
                    }
                    return true;
                }
                return false;
            }
        }).send();
    },

    /**
     * 加载更多事件
     */
    loadMore: function () {
        this._requestMyClerks();
    },

    /**
     * 请求二维码
     * @param salesmanId
     * @private
     */
    _requestInviteClerk(userId) {
        let params = {
            agentId: app.globalData.userData.agentId,
            userId: userId
        };
        network_utils.buildRequest(constants.Interfaces.FUNC_QR_SALESMAN, params, {
            doHttpSucess: (response) => {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    wx.navigateTo({
                        url: 'inviteClerks/inviteClerks?userId=' + userId + '&qrUrl=' + encodeURIComponent(response['qrUrl']),
                    });
                }
            }
        }).send();
    },

    /**
     * 处理数组，排序和过滤
     * @param dataArr
     * @private
     */
    _processData: function (dataArr) {
        if (!dataArr || dataArr.length === 0) {
            return [];
        }

        for (var it = 0; it < dataArr.length; it++) {
            dataArr[it].permissionConf = JSON.stringify(dataArr[it].permissionConf)
        }
        let helpers = {
            _sort: (a, b) => {
                if (a.salesmanStatus === '1') {
                    return -1;
                } else if (b.salesmanStatus === '1') {
                    return 1;
                }

                if (a.salesmanStatus === '4') {
                    return -1;
                } else if (b.salesmanStatus === '4') {
                    return 1;
                }
            },

            _filter: (item) => {
                //过滤掉自己
                return item.userId !== app.globalData.userData.userId;
            }
        };

        return dataArr.filter(helpers._filter).sort(helpers._sort);
    }
});